<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractChuJieModel;
use app\index\model\ContractChuModel;
use app\index\model\ContractKuanListModel;
use app\index\model\ContractKuanModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiGongModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginModel;
use app\index\model\EnginShuiModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\HouChujinGuanModel;
use app\index\model\HouChujinListModel;
use app\index\model\HouDayzhiGuanModel;
use app\index\model\HouDayzhiListModel;
use app\index\model\HouGonghuiListModel;
use app\index\model\HouGonghuiModel;
use app\index\model\HouGongshuiGuanModel;
use app\index\model\HouGongyuModel;
use app\index\model\HouGongzhiGuanModel;
use app\index\model\HouGongzhiListModel;
use app\index\model\HouKouQifeiModel;
use app\index\model\HouKouShuifeiModel;
use app\index\model\PaymentEnginListModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PiaoChuListModel;
use app\index\model\PiaoChuModel;
use app\index\model\PiaoDayListModel;
use app\index\model\PiaoDayModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoKouListModel;
use app\index\model\PiaoKouModel;
use app\index\model\PiaoPutListModel;
use app\index\model\PiaoPutModel;
use app\index\model\PiaoPutPayModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxListModel;
use app\index\model\PiaoTaxModel;
use app\index\model\StuffRuModel;
use app\index\model\UserModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BudgetService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\PiaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Piao extends Common
{
    public function detail_piao_kai(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }
        if(isset($param['piaolistid']) && !empty($param['piaolistid'])){
            $where[] = ['list.id','=', $param['piaolistid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|e.name|e.number|e.gofuer|kai.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }

        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['kai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['kai.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['qianpiaotype']) && !empty($param['qianpiaotype'])){
            $where[] = ['kai.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['ishong']) && $param['ishong'] != ''){
            $where[] = ['kai.ishong','=', $param['ishong']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';

        $data['data'] = PiaoKaiListModel::alias('list')
            ->field('kai.*,e.name as enginname,bill.sericnum,e.areas,
            list.id as listid,list.nums,list.fee,list.rate,list.ratefee as listratefee,
            list.money as listmoney,list.explain as listexplain')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->leftJoin('flow_bill bill','bill.id=kai.billid')
            ->leftJoin('engin e','e.id=kai.enginid')
            ->where($where)
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

                $item['yihuikuan'] = HouGonghuiListModel::alias('list')
                    ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                    ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                    ->where(['list.piaoid'=>$item['listid']])
                    ->sum('list.xiaofee');

                if($item['yihuikuan'] <= 0){
                    $item['statename'] = '未回款';
                }else{
                    if($item['yihuikuan'] < $item['listmoney']){
                        $item['statename'] = '部分回款';
                    }else{
                        $item['statename'] = '已回款';
                    }
                }
                $item['daihuikuan'] = round($item['listmoney'] - $item['yihuikuan'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.gofuer|kai.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['kai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['kai.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['ishong']) && $param['ishong'] != ''){
            $where[] = ['kai.ishong','=', $param['ishong']];
        }
        if(isset($param['iskaishe']) && $param['iskaishe'] != ''){
            $where[] = ['kai.iskaishe','=', $param['iskaishe']];
        }
        if(isset($param['waijiao']) && !empty($param['waijiao'])){
            $where[] = ['kai.waijiao','=', $param['waijiao']];
        }

        if(isset($param['qianpiaotype']) && !empty($param['qianpiaotype'])){
            $where[] = ['kai.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['kaier']) && !empty($param['kaier'])){
            $where[] = ['kai.kaier','=', $param['kaier']];
        }
        if(isset($param['kuantype']) && !empty($param['kuantype'])){
            $where[] = ['kai.kuantype','=', $param['kuantype']];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['kaier'] = (new UserService())->get_chaninfo_all();
        $data['is_kaier'] = isset($param['kaier']) ? $param['kaier'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';

        $data['kuantype'] = (new OptService())->get_base_info('cai_kuantype');
        $data['is_kuantype'] = isset($param['kuantype']) ? $param['kuantype'] : '';

        $data['waijiao'] = (new PiaoService())->get_kai_waijiao();
        $data['is_waijiao'] = isset($param['waijiao']) ? $param['waijiao'] : '';

        $whereState = "";
        if(isset($param['piao_state']) && !empty($param['piao_state'])){
            if($param['piao_state'] == 1){
                $whereState = "kai.dt is null or kai.dt='' or kai.dt='0000-00-00'";
            }
            if($param['piao_state'] == 2){
                $whereState = "kai.dt is not null and kai.dt != '' and kai.dt != '0000-00-00'";
            }
        }

        $data['data'] = PiaokaiModel::alias('kai')
            ->field('kai.*,e.name as enginname,e.areas,gui_unit.name as gui_unit_name')
            ->join('engin e','e.id=kai.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->where($whereState)
            ->where((new MenuService())->get_miss_where('piao_kai', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
                $item['shuilv_name'] = (new OptService())->get_base_showname('cai_piao_shuilv', $item['shuilv']);
                $item['kuantype_name'] = (new OptService())->get_base_showname('cai_kuantype', $item['kuantype']);
                $item['funei_name'] = (new OptService())->get_base_showname('cai_piao_funei', $item['funei']);
                $item['kaier'] = DeptModel::get_name_by_pk($item['kaier']);

                $item['shuishou_name'] = '';
                if($item['shuishou'] == 1){
                    $item['shuishou_name'] = '开票已预缴';
                }
                if($item['shuishou'] == 2){
                    $item['shuishou_name'] = '回款待收取';
                }

                $item['piao_state'] = '未开票';
                if($item['dt'] && $item['dt'] != '0000-00-00'){
                    $item['piao_state'] = '已开票';
                }
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_kai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择工程名称');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入开票金额');
            }
            if(empty($param['duier'])){
                return outputJson(4000, '请选择 对方单位');
            }
            if(empty($param['ratenum'])){
                return outputJson(4000, '请输入纳税人识别号');
            }

            $param['number'] = (new PiaoService())->create_piao_kai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);



            Db::startTrans();
            try {
                $id = PiaokaiModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['kaiid'] = $id;
                        PiaoKaiListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_kai',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_kai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_kai', $id, '开票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['number'] = (new PiaoService())->create_piao_kai_number();
            $data['files'] = (new OptService())->get_file('piao_kai');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = PiaokaiModel::where(['id'=>$param['id']])->find();

            if(empty($param['money'])){
                return outputJson(4000, '请输入开票金额');
            }
            if(empty($param['duier'])){
                return outputJson(4000, '请选择对方单位');
            }
            if(empty($param['ratenum'])){
                return outputJson(4000, '请输入纳税人识别号');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                PiaokaiModel::update($param);

                PiaoKaiListModel::where(['kaiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['kaiid'] = $param['id'];
                        PiaoKaiListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_kai', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_kai', $param['id'], '开票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])->value('name');
            $data['engininfo'] = $engininfo;



            $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function kai_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = PiaokaiModel::where(['id'=>$param['id']])->find();



            if(empty($param['dt'])){
                return outputJson(4000, '请选择开票日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入发票明细');
            }



            Db::startTrans();
            try {
                PiaokaiModel::update($param);

                PiaoKaiListModel::where(['kaiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['kaiid'] = $param['id'];
                        $xiaofeeobj = json_decode($item['xiaofeeobj'], true) ?: [];
                        unset($item['xiaofeeobj']);


                        $listid = PiaoKaiListModel::insertGetId($item);


                        foreach($xiaofeeobj as $vvvv){
                            HouGonghuiListModel::insert([
                                'type' => 2,
                                'gonghuiid' => $vvvv['gonghuiid'],
                                'xiaofee' => $vvvv['xiaofee'],
                                'piaoid' => $listid,
                            ]);
                        }
                    }
                }

                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                (new OptService())->add_flow_log('开票', 'piao_kai', $param['id']);
                (new OptService())->add_log_list('开票', 'piao_kai', $param['id'], '销项票开具');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['data']['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $data['data']['qianpiaotype']);
            $data['data']['shuilv_name'] = (new OptService())->get_base_showname('cai_piao_shuilv', $data['data']['shuilv']);
            $data['data']['kuantype_name'] = (new OptService())->get_base_showname('cai_kuantype', $data['data']['kuantype']);
            $data['data']['funei_name'] = (new OptService())->get_base_showname('cai_piao_funei', $data['data']['funei']);

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $xiaofeeList = HouGonghuiListModel::field('gonghuiid,piaoid,xiaofee')
                    ->where(['type'=>2,'piaoid'=>$item['id']])->select()->toArray();
                $item['xiaofeeobj'] = json_encode($xiaofeeList);
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_piao_kai(Request $request){
        $param = $request->post();
        $data = PiaokaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaokaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_kai', $data['id'], '开票');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_kai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaokaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['kaier'] = DeptModel::get_name_by_pk($data['data']['kaier']);
        $data['data']['shuishou_name'] = $data['data']['shuishou'] == 1 ? '开票已预缴' : '回款待收取';

        if($data['data']['shuishou'] == 1){
            $kouidsArr = HouKouQifeiModel::where('id','in',$data['data']['kouids'])->select()->toArray();
            foreach ($kouidsArr as &$kkvv){
                if($kkvv['type'] == 1){
                    $billid = HouGonghuiModel::where(['id'=>$kkvv['zhuid']])->value('billid');
                }else{
                    $billid = HouGongyuModel::where(['id'=>$kkvv['zhuid']])->value('billid');
                }
                $kkvv['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$billid])->value('sericnum');
            }
            $data['kouidsArr'] = $kouidsArr;
        }else{
            $shuiidinfo = EnginShuiModel::where(['id'=>$data['data']['shuiid']])->find();
            $data['shuiidinfo'] = $shuiidinfo;
        }

        $data['data']['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $data['data']['qianpiaotype']);
        $data['data']['shuilv_name'] = (new OptService())->get_base_showname('cai_piao_shuilv', $data['data']['shuilv']);
        $data['data']['kuantype_name'] = (new OptService())->get_base_showname('cai_kuantype', $data['data']['kuantype']);
        $data['data']['funei_name'] = (new OptService())->get_base_showname('cai_piao_funei', $data['data']['funei']);

        $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['yihuikuan'] = HouGonghuiListModel::alias('list')
                ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                ->where(['list.piaoid'=>$item['id']])
                ->sum('list.xiaofee');

            $item['statename'] = '';
            if($item['yihuikuan'] <= 0){
                $item['statename'] = '未回款';
            }else{
                if($item['yihuikuan'] < $item['money']){
                    $item['statename'] = '部分回款';
                }else{
                    $item['statename'] = '已回款';
                }
            }
            $item['daihuikuan'] = round($item['money'] - $item['yihuikuan'], 2);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    //销项票开具 - 多选选择【工程负责人预收款-收取税款明细(过滤已关联数据)】
    public function list_koushou_shuifei(Request $request){
        $enginid = $request->post('enginid', 0, 'int');

        $list = [];
        $yu_kouList = HouKouQifeiModel::alias('list')
            ->field('list.*,hou.billid')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2,'list.houkoulei'=>2,'list.enginid'=>$enginid])
            ->select()->toArray();
        foreach ($yu_kouList as &$item){
            $isXuan = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['shuishou'=>1])
                ->where(Db::raw("find_in_set(".$item['id'].",`kouids`)"))
                ->find();
            if($isXuan){
                continue;
            }
            $item['kouid'] = $item['id'];
            $item['yushouid'] = $item['zhuid'];

            $item['shulai'] = '工程负责人预收款-收取';
            $item['enginname'] = EnginModel::where(['id'=>$item['enginid']])->value('name');
            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
            $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
            $list[] = $item;
        }


        $hui_kouList = HouKouQifeiModel::alias('list')
            ->field('list.*,hou.billid')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1,'list.houkoulei'=>2,'list.enginid'=>$enginid])
            ->select()->toArray();
        foreach ($hui_kouList as &$item){
            $isXuan = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['shuishou'=>1])
                ->where(Db::raw("find_in_set(".$item['id'].",`kouids`)"))
                ->find();
            if($isXuan){
                continue;
            }
            $item['kouid'] = $item['id'];
            $item['yushouid'] = $item['zhuid'];

            $item['shulai'] = '工程回款-收取';
            $item['enginname'] = EnginModel::where(['id'=>$item['enginid']])->value('name');
            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
            $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
            $list[] = $item;
        }


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piaokai_engin_shui(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $where[] = ['shui.deleted_user','=', 0];
        $where[] = ['shui.comid','=',$this->userinfo['comid']];
        $where[] = ['shui.isturn','=',1];
        $where[] = ['shui.status','=',1];
        $where[] = ['shui.enginid','=',$enginid];


        $list = [];
        $info = EnginShuiModel::alias('shui')
            ->field('shui.*,e.name as enginname')
            ->join('engin e','e.id=shui.enginid')
            ->where($where)->select()->toArray();
        foreach ($info as $item){
            $isXuan = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['shuishou'=>2,'shuiid'=>$item['id']])
                ->find();
            if($isXuan){
                continue;
            }
            $item['qiantype_name'] = (new OptService())->get_base_showname('contract_nei_qiantype', $item['qiantype']);
            $item['state_name'] = (new EnginService())->get_engin_shui_state_name($item['state']);
            $list[] = $item;
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function detail_piao_chu(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['chu.deleted_user','=', 0];
        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['chu.isturn','=',1];
        $where[] = ['chu.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|chu.lianer|list.xiaoer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['chu.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['xiaoer']) && !empty($param['xiaoer'])){
            $where[] = ['list.xiaoer','like', '%'.$param['xiaoer'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['qianpiaotype']) && $param['qianpiaotype'] != ''){
            $where[] = ['list.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['chu.status','=', $param['status']];
        }

        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['list.kaidt','between', explode(' - ', $param['kaidt'])];
        }

        if(isset($param['usedt']) && !empty($param['usedt'])){
            $where[] = ['list.usedt','between', explode(' - ', $param['usedt'])];
        }

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';

        $data['data'] = PiaoChuListModel::alias('list')
            ->field('chu.*,con.name as conname,con.number as connumber,bill.sericnum,
            list.id as listid,list.chuid,list.xiaoer,list.qianpiaotype,list.nums,list.kaidt,list.cailei,
            list.fee,list.rate,list.ratefee as listratefee,list.money as listmoney,list.huidt,list.explain as listexplain,
            list.difee,list.usedt')
            ->join('piao_chu chu','chu.id=list.piaochuid')
            ->leftJoin('flow_bill bill','bill.id=chu.billid')
            ->leftJoin('contract_chu con','con.id=list.chuid')
            ->where($where)
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

                $item['yifufee'] = HouChujinGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_chujin jin','jin.id=guan.chujinid')
                    ->join('piao_chu piao','piao.id=guan.piaoid')
                    ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.piaolistid'=>$item['listid']])
                    ->sum('difee');
                if($item['yifufee'] <= 0){
                    $item['statename'] = '未付款';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分付款';
                    }else{
                        $item['statename'] = '已付款';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.deleted_user','=', 0];
        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['chu.isturn','=',1];
        $where[] = ['chu.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['chu.lianer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['chu.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['chu.status','=', $param['status']];
        }



        $data['data'] = PiaoChuModel::alias('chu')
            ->field('chu.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_chu', 'chu'))
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_chu');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function use_piao_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                PiaoChuListModel::where('id','in',$param['ids'])->update(['usedt'=>$param['usedt']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_piao_chu(Request $request){
        $param = $request->post();
        $data = PiaoChuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoChuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_chu', $data['id'], '储备进项票登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_chu(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoChuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $list = PiaoChuListModel::where(['piaochuid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['chuid_name'] = ContractChuModel::where(['id'=>$item['chuid']])->value('name');
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
        }
        $data['list'] = $list;



        $data['files'] = (new OptService())->get_file('piao_chu', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_hou_chujin_guan(Request $request){
        $fuer = $request->post('xiaoer', '', 'string');
        if(!$fuer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $chuList = HouChujinListModel::alias('list')
            ->field('list.*,hou.billid')
            ->join('hou_chujin hou','hou.id=list.chujinid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.fuer'=>$fuer])
            ->select()->toArray();
        foreach ($chuList as $item){
            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
            $item['status_name'] = '审批通过';

            $item['chujinid'] = $item['chujinid'];
            $item['chujinlistid'] = $item['id'];
            $item['chuid_name'] = ContractChuModel::where(['id'=>$item['chuid']])->value('name');

            $item['yishoupiao'] = HouChujinGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_chujin jin','jin.id=guan.chujinid')
                ->join('piao_chu piao','piao.id=guan.piaoid')
                ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.chujinlistid'=>$item['id']])
                ->sum('difee');

            if($item['yishoupiao'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yishoupiao'] < $item['money']){
                    $item['statename'] = '部分收票';
                }else{
                    continue;
                }
            }
            $item['daishoufee'] = round($item['money'] - $item['yishoupiao'], 2);

            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_piao_kai_guan(Request $request){
        $enginid = $request->post('enginid', 0, 'int');

        $list = [];
        $chuList = HouGonghuiModel::alias('hou')
            ->field('hou.*')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['hou.enginid'=>$enginid])
            ->select()->toArray();

        foreach ($chuList as $item){
            $item['gonghuiid'] = $item['id'];

            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');

            $item['yikaifee'] = HouGonghuiListModel::alias('list')
                ->field('list.*')
                ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                ->where(['list.gonghuiid'=>$item['id']])
                ->sum('list.xiaofee');

            if($item['yikaifee'] <= 0){
                $item['statename'] = '未开票';
            }else{
                if($item['yikaifee'] < $item['money']){
                    $item['statename'] = '部分开票';
                }else{
                    continue;
                }
            }
            $item['daikaifee'] = round($item['money'] - $item['yikaifee'], 2);
            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }





    public function detail_piao_shou(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|e.name|e.gofuer|list.nums|list.xiaoer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum|e.name|e.number|e.gofuer|shou.flowuser','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['qianpiaotype']) && $param['qianpiaotype'] != ''){
            $where[] = ['list.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['usedt']) && !empty($param['usedt'])){
            $where[] = ['list.usedt','between', explode(' - ', $param['usedt'])];
        }
        if(isset($param['xiaoer']) && !empty($param['xiaoer'])){
            $where[] = ['list.xiaoer','like', '%'.$param['xiaoer'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['list.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['huidt']) && !empty($param['huidt'])){
            $where[] = ['list.huidt','between', explode(' - ', $param['huidt'])];
        }
        if(isset($param['zhikuanlei']) && $param['zhikuanlei'] != ''){
            $where[] = ['list.zhikuanlei','=', $param['zhikuanlei']];
        }


        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';

        $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
        $data['is_zhikuanlei'] = isset($param['zhikuanlei']) ? $param['zhikuanlei'] : '';

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['data'] = PiaoShouListModel::alias('list')
            ->field('shou.*,e.name as enginname,bill.sericnum,cai.name as cainame,
            list.id as listid,list.xiaoer,list.qianpiaotype,list.nums,list.kaidt,list.fee,list.rate,list.ratefee as listratefee,
            list.money as listmoney,list.explain as listexplain,list.zhikuanlei,list.huidt,list.difee,list.caiid,list.usedt')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('contract_cai cai','cai.id=list.caiid')
            ->where($where)
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
                $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);

                $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                    ->join('piao_shou piao','piao.id=guan.piaoid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.piaolistid'=>$item['listid']])
                    ->sum('difee');

                if($item['yifufee'] <= 0){
                    $item['statename'] = '未付款';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分付款';
                    }else{
                        $item['statename'] = '已付款';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);


                $item['tuishui'] = '未退税';
                $yiguan = HouGongshuiGuanModel::alias('guan')
                    ->join('hou_gongshui shui','shui.id=guan.gongshuiid')
                    ->join('piao_shou piao','piao.id=guan.piaoid')
                    ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.piaolistid'=>$item['listid']])
                    ->find();
                if($yiguan){
                    $item['tuishui'] = '已退税';
                }

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.gofuer|shou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['data'] = PiaoShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,e.number as enginnumber')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['dt'])){
                return outputJson(4000, '请选择收票日期');
            }

            $param['number'] = (new PiaoService())->create_piao_shou_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入发票明细');
            }


            Db::startTrans();
            try {
                $id = PiaoShouModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shouid'] = $id;
                        PiaoShouListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_shou',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_shou', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_shou', $id, '收票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['shouer_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');

            $data['piaotype'] = (new PiaoService())->get_shou_piaotype();
            $data['number'] = (new PiaoService())->create_piao_shou_number();
            $data['files'] = (new OptService())->get_file('piao_shou');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = PiaoShouModel::where(['id'=>$param['id']])->find();


            if(empty($param['dt'])){
                return outputJson(4000, '请选择收票日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入发票明细');
            }

            Db::startTrans();
            try {
                PiaoShouModel::update($param);

                PiaoShouListModel::where(['shouid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shouid'] = $param['id'];
                        PiaoShouListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_shou',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_shou', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_shou', $param['id'], '收票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['shouer_info'] = (new UserService())->get_users_part_arr($data['data']['shouer'], 'user');

            $list = PiaoShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['piaotype_name'] = (new PiaoService())->get_shou_piaotype_name($item['piaotype']);
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
            }
            $data['list'] = $list;

            $data['piaotype'] = (new PiaoService())->get_shou_piaotype();
            $data['files'] = (new OptService())->get_file('piao_shou', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function use_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                PiaoShouListModel::where('id','in',$param['ids'])->update(['usedt'=>$param['usedt']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_piao_shou(Request $request){
        $param = $request->post();
        $data = PiaoShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_shou', $data['id'], '进项票登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $list = PiaoShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
            $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);


            $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                ->join('piao_shou piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->sum('difee');

            if($item['yifufee'] <= 0){
                $item['statename'] = '未付款';
            }else{
                if($item['yifufee'] < $item['money']){
                    $item['statename'] = '部分付款';
                }else{
                    $item['statename'] = '已付款';
                }
            }
            $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);


            $item['tuishui'] = '未退税';
            $yiguan = HouGongshuiGuanModel::alias('guan')
                ->join('hou_gongshui shui','shui.id=guan.gongshuiid')
                ->join('piao_shou piao','piao.id=guan.piaoid')
                ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->find();
            if($yiguan){
                $item['tuishui'] = '已退税';
            }
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_gong_zhi_piaoshou_guan(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $fuer = $request->post('fuer', '', 'string');
        if(!$fuer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $chuList = HouGongzhiListModel::alias('list')
            ->field('list.*,hou.billid')
            ->join('hou_gongzhi hou','hou.id=list.gongzhiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['hou.enginid'=>$enginid,'list.fuer'=>$fuer])
            ->select()->toArray();

        foreach ($chuList as $item){
            $item['gongzhiid'] = $item['gongzhiid'];
            $item['gongzhilistid'] = $item['id'];

            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
            $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

            $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                ->join('piao_shou piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.gongzhilistid'=>$item['id']])
                ->sum('difee');

            if($item['yifufee'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yifufee'] < $item['money']){
                    $item['statename'] = '部分收票';
                }else{
                    continue;
                }
            }
            $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);
            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function detail_piao_day(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['day.deleted_user','=', 0];
        $where[] = ['day.comid','=',$this->userinfo['comid']];
        $where[] = ['day.isturn','=',1];
        $where[] = ['day.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|list.xiaoer|day.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['day.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['xiaoer']) && !empty($param['xiaoer'])){
            $where[] = ['list.xiaoer','like', '%'.$param['xiaoer'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['listexplain']) && !empty($param['listexplain'])){
            $where[] = ['list.explain','like', '%'.$param['listexplain'].'%'];
        }

        if(isset($param['qianpiaotype']) && $param['qianpiaotype'] != ''){
            $where[] = ['list.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['day.status','=', $param['status']];
        }

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';

        $data['data'] = PiaoDayListModel::alias('list')
            ->field('day.*,bill.sericnum,
            list.id as listid,list.xiaoer,list.qianpiaotype,list.nums,list.kaidt,list.fee,list.rate,list.ratefee as listratefee,
            list.money as listmoney,list.explain as listexplain,list.huidt,list.difee,list.usedt')
            ->join('piao_day day','day.id=list.dayid')
            ->leftJoin('flow_bill bill','bill.id=day.billid')
            ->where($where)
            ->order('day.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

                $item['yifufee'] = HouDayzhiGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                    ->join('piao_day piao','piao.id=guan.piaoid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.piaolistid'=>$item['listid']])
                    ->sum('difee');

                if($item['yifufee'] <= 0){
                    $item['statename'] = '未付款';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分付款';
                    }else{
                        $item['statename'] = '已付款';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_piao_day(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['day.deleted_user','=', 0];
        $where[] = ['day.comid','=',$this->userinfo['comid']];
        $where[] = ['day.isturn','=',1];
        $where[] = ['day.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['day.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['day.title','like', '%'.$param['title'].'%'];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['day.status','=', $param['status']];
        }

        if(isset($param['kuantype']) && !empty($param['kuantype'])){
            $where[] = ['day.kuantype','=', $param['kuantype']];
        }

        $data['kuantype'] = (new OptService())->get_base_info('cai_kuantype');
        $data['is_kuantype'] = isset($param['kuantype']) ? $param['kuantype'] : '';


        $data['data'] = PiaoDayModel::alias('day')
            ->field('day.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_day', 'day'))
            ->order('day.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['kuantype_name'] = (new OptService())->get_base_showname('cai_kuantype', $item['kuantype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_day');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function use_piao_day(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                PiaoDayListModel::where('id','in',$param['ids'])->update(['usedt'=>$param['usedt']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_piao_day(Request $request){
        $param = $request->post();
        $data = PiaoDayModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoDayModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_day', $data['id'], '日常进项票登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_day(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoDayModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['kuantype_name'] = (new OptService())->get_base_showname('cai_kuantype', $data['data']['kuantype']);

        $list = PiaoDayListModel::where(['dayid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
            $item['dayzhiid'] = 0;
            $exist = HouDayzhiGuanModel::alias('guan')
                ->field('guan.*')
                ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                ->join('piao_day piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->find();
            if($exist){
                $item['dayzhiid'] = $exist['dayzhiid'];
            }
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_day', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_day_zhi_piaoday_guan(Request $request){
        $fuer = $request->post('fuer', '', 'string');
        if(!$fuer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $chuList = HouDayzhiListModel::alias('list')
            ->field('list.*,hou.billid')
            ->join('hou_dayzhi hou','hou.id=list.dayzhiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.fuer'=>$fuer])
            ->select()->toArray();

        foreach ($chuList as $item){
            $item['dayzhiid'] = $item['dayzhiid'];
            $item['dayzhilistid'] = $item['id'];

            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
            $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

            $item['yifufee'] = HouDayzhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                ->join('piao_day piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.dayzhilistid'=>$item['id']])
                ->sum('difee');

            if($item['yifufee'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yifufee'] < $item['money']){
                    $item['statename'] = '部分收票';
                }else{
                    continue;
                }
            }
            $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);
            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function list_piao_tax(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tax.deleted_user','=', 0];
        $where[] = ['tax.comid','=',$this->userinfo['comid']];
        $where[] = ['tax.isturn','=',1];
        $where[] = ['tax.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|tax.number|tax.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tax.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['tax.month','like', '%'.$param['month'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['tax.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tax.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = PiaoTaxModel::alias('tax')
            ->field('tax.*,e.name as enginname')
            ->join('engin e','e.id=tax.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_tax', 'tax'))
            ->order('tax.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_tax');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_piao_tax(Request $request){
        $param = $request->post();
        $data = PiaoTaxModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoTaxModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_tax', $data['id'], '税款预缴');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_tax(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoTaxModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $list = PiaoTaxListModel::where(['taxid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['ratetype'] = (new OptService())->get_base_showname('cai_ratetype', $item['ratetype']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_tax', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_piao_kai_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','=',1];


        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['kai.piaotype','=', $param['piaotype']];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kai.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['kai.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['kai.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['kai.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['unitaddr']) && !empty($param['unitaddr'])){
            $where[] = ['kai.unitaddr','like', '%'.$param['unitaddr'].'%'];
        }
        if(isset($param['phone']) && !empty($param['phone'])){
            $where[] = ['kai.phone','like', '%'.$param['phone'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['kai.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';


        $data['data'] = PiaoKaiListModel::alias('list')
            ->field('list.*,e.name as enginname,kai.piaotype,kai.duier,kai.dt,kai.number')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->join('engin e','e.id=kai.enginid')
            ->where((new MenuService())->get_miss_where('piao_kai_hui', 'kai'))
            ->where($where)
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_piao_shou_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','=',1];

        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['list.caiid','=', $param['caiid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shou.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['list.piaotype','=', $param['piaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['list.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['piaotype'] = (new PiaoService())->get_shou_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';


        $data['data'] = PiaoShouListModel::alias('list')
            ->field('list.*,e.name as enginname,shou.dt,shou.number,cai.name as cainame')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('contract_cai cai','cai.id=list.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou_hui', 'list'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype'] = (new PiaoService())->get_shou_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function list_piao_engin_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.business')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_engin_hui', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['faer'] = '';
                $item['chenger'] = '';
                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $item['faer'] = $shiinfo['faer'];
                    $item['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
                }




                $item['kai_all'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['kai_nofee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.fee');
                $item['kai_ratefee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.ratefee');


                $item['shou_all'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['shou_jin'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->where(['list.piaotype'=>1])
                    ->sum('list.ratefee');

                $item['yingfee'] = round($item['kai_ratefee'] - $item['shou_jin'], 2);
                $item['yingfee'] = $item['yingfee'] < 0 ? 0 : $item['yingfee'];

                $item['taxfee'] = PiaoTaxModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }




    public function list_piao_put(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['put.deleted_user','=', 0];
        $where[] = ['put.comid','=',$this->userinfo['comid']];
        $where[] = ['put.isturn','=',1];
        $where[] = ['put.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['put.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|put.number|put.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['put.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['put.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $dtArr = explode(' - ', $param['created_at']);
            $where[] = ['put.created_at','between', [$dtArr[0].' 00:00:00', $dtArr[1].' 23:59:59']];
        }
        if(isset($param['ispay']) && $param['ispay'] != ''){
            $where[] = ['put.ispay','=', $param['ispay']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['put.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = PiaoPutModel::alias('put')
            ->field('put.*,e.name as enginname')
            ->join('engin e','e.id=put.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_put', 'put'))
            ->order('put.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['ispay_name'] = $item['ispay'] > 0 ? '是' : '否';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_put');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_piao_put(Request $request){
        $param = $request->post();
        $data = PiaoPutModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoPutModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_put', $data['id'], '收票登记申请');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_put(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoPutModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;
        $shimoney = 0;
        $shiinfo = ContractShiModel::where(['enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1])
            ->where('status','in',[0,1])->find();
        if($shiinfo){
            $shimoney = $shiinfo['money'];
        }
        $data['shiinfo'] = $shiinfo;



        $list = PiaoPutListModel::where(['putid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['enginid'] = $data['data']['enginid'];
            $item['piaotype_name'] = (new PiaoService())->get_put_piaotype_name($item['piaotype']);
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['filesnum'] = count($item['files']);

            $item['leiputfee'] = PiaoPutListModel::alias('list')
                ->join('piao_put zhu','zhu.id=list.putid')
                ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                ->where(['zhu.enginid'=>$data['data']['enginid'],'list.gonger'=>$item['gonger']])
                ->sum('list.money');

            $item['jinbi'] = 0;
            if($shimoney != 0){
                $item['jinbi'] = round($item['leiputfee']/$shimoney*100, 2);
            }

        }
        $data['list'] = $list;

        $payinfo = PiaoPutPayModel::where(['putid'=>$data['data']['id']])->select()->toArray();
        foreach ($payinfo as &$item){
            $pay = PaymentEnginModel::where(['id'=>$item['payenid']])->find();
            $item['number'] = $pay['number'];
            $item['gonger'] = $pay['gonger'];
            $item['money'] = $pay['money'];
            $item['payfee'] = $pay['payfee'];
            $item['qingdt'] = date("Y-m-d", strtotime($pay['created_at']));
            $item['paydt'] = AccountListModel::where(['type'=>3,'mid'=>$pay['id']])->order('dt DESC')->value('dt');
            $item['paystatus'] = (new PaymentService())->get_paydata_paystatus_name($pay['money'], $pay['payfee']);
        }
        $data['payinfo'] = $payinfo;






        $data['files'] = (new OptService())->get_file('piao_put', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function ming_piao_put(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['put.deleted_user','=', 0];
        $where[] = ['put.comid','=',$this->userinfo['comid']];
        $where[] = ['put.isturn','=',1];
        $where[] = ['put.status','=',1];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['put.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|put.number|put.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['list.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['put.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['put.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $dtArr = explode(' - ', $param['created_at']);
            $where[] = ['put.created_at','between', [$dtArr[0].' 00:00:00', $dtArr[1].' 23:59:59']];
        }
        if(isset($param['ispay']) && $param['ispay'] != ''){
            $where[] = ['put.ispay','=', $param['ispay']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['put.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = PiaoPutListModel::alias('list')
            ->field('put.*,e.name as enginname,list.id as listid,list.gonger,list.dt,list.piaotype,list.nums,list.fee
            ,list.rate,list.ratefee,list.money,list.dengdt,list.files,list.explain')
            ->join('piao_put put','put.id=list.putid')
            ->join('engin e','e.id=put.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_put', 'put'))
            ->order('put.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['ispay_name'] = $item['ispay'] > 0 ? '是' : '否';

                $item['kou_ratefee'] = PiaoKouListModel::alias('list')
                    ->join('piao_kou zhu','zhu.id=list.kouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['list.listid'=>$item['listid']])
                    ->sum('ratefee');
                $item['kou_sheng'] = round($item['ratefee'] - $item['kou_ratefee'], 2);
                if(!$item['kou_ratefee']){
                    $item['kou_status'] = '未抵扣';
                }else{
                    if($item['kou_sheng'] > 0){
                        $item['kou_status'] = '部分抵扣';
                    }else{
                        $item['kou_status'] = '全额抵扣';
                    }
                }

                $item['files_num'] = FileModel::where('id','in',$item['files'])->count();
                $item['piaotype_name'] = (new PiaoService())->get_put_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }



    public function list_piao_kou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kou.deleted_user','=', 0];
        $where[] = ['kou.comid','=',$this->userinfo['comid']];
        $where[] = ['kou.isturn','=',1];
        $where[] = ['kou.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kou.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kou.number|kou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['rendt']) && !empty($param['rendt'])){
            $where[] = ['kou.rendt','between', explode(' - ', $param['rendt'])];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['kou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $dtArr = explode(' - ', $param['created_at']);
            $where[] = ['put.created_at','between', [$dtArr[0].' 00:00:00', $dtArr[1].' 23:59:59']];
        }


        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kou.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = PiaoKouModel::alias('kou')
            ->field('kou.*,e.name as enginname')
            ->join('engin e','e.id=kou.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kou', 'kou'))
            ->order('kou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['xiaofee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['enginid']])
                    ->sum('list.ratefee');

                $item['leiratefee'] = PiaoKouListModel::alias('list')
                    ->join('piao_kou zhu','zhu.id=list.kouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['enginid']])
                    ->sum('list.ratefee');


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_kou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_piao_kou(Request $request){
        $param = $request->post();
        $data = PiaoKouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoKouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_kou', $data['id'], '成本票抵扣');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_kou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoKouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $shiinfo = ContractShiModel::where(['enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1])
            ->where('status','in',[0,1])->find();
        $data['shiinfo'] = $shiinfo;

        $data['xiaofee'] = PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where(['zhu.enginid'=>$data['data']['enginid']])
            ->sum('list.ratefee');

        $data['wangmoney'] = PiaoKouListModel::alias('list')
            ->join('piao_kou zhu','zhu.id=list.kouid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where(['zhu.enginid'=>$data['data']['enginid']])
            ->sum('list.ratefee');


        $data['leimoney'] = round($data['wangmoney'] + $data['data']['money'], 2);


        $list = PiaoKouListModel::where(['kouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['ben_ratefee'] = $item['ratefee'];
            $listinfo = PiaoPutListModel::where(['id'=>$item['listid']])->find();

            $item['nums'] = $listinfo['nums'];
            $item['gonger'] = $listinfo['gonger'];
            $item['dt'] = $listinfo['dt'];
            $item['dengdt'] = $listinfo['dengdt'];
            $item['piaotype_name'] = (new PiaoService())->get_put_piaotype_name($listinfo['piaotype']);
            $item['fee'] = $listinfo['fee'];
            $item['rate'] = $listinfo['rate'];
            $item['ratefee'] = $listinfo['ratefee'];
            $item['money'] = $listinfo['money'];
            $item['filearr'] = FileModel::where('id','in',$listinfo['files'])->select()->toArray();
            $item['files'] = FileModel::where('id','in',$listinfo['files'])->select()->toArray();
            $item['filesnum'] = count($item['files']);

            $item['kou_ratefee'] = PiaoKouListModel::alias('list')
                ->join('piao_kou zhu','zhu.id=list.kouid')
                ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1])
                ->where('zhu.status','in',[0,1])
                ->where(['list.listid'=>$item['listid']])
                ->sum('ratefee');
            $item['ke_ratefee'] = round($item['ratefee'] - $item['kou_ratefee'], 2);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_kou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }












}
